package me.keiwu.user.controller.pub;

import com.alibaba.fastjson.JSON;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.user.domain.Notice;
import me.keiwu.user.service.NoticeService;
import me.keiwu.user.service.UserDeliveryAddressService;
import me.keiwu.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pub/userAddress")
public class UserPubAddressController {

    @Autowired
    private UserDeliveryAddressService userDeliveryAddressService;

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/sendAddressTmpMsg")
    public ResponseV2 sendAddressTmpMsg(Long deliveryUserId, String gift, String msg) {
        return userDeliveryAddressService.sendDeliveryAddressTmpMsg(deliveryUserId, gift, msg);
    }

    @ResponseBody
    @RequestMapping("/sendAddressTmpMsgList")
    public ResponseV2 sendAddressTmpMsgList(String userIds, String gift, String msg) {
        if(StringUtils.isBlank(userIds)) {
            return ResponseV2.fail("id 不能为空");
        }
        if(StringUtils.isBlank(gift)) {
            return ResponseV2.fail("gift 不能为空");
        }
        if(StringUtils.isBlank(msg)) {
            return ResponseV2.fail("msg 不能为空");
        }
        ResponseV2 finalResp = ResponseV2.ok();
        List<ResponseV2> childResps = new ArrayList<ResponseV2>();

        List<Long> idArray = JSON.parseArray(userIds,Long.class);
        if (idArray != null && idArray.size() > 0) {
            for (Long uid : idArray) {
                if (uid != null) {
                    ResponseV2 childResp = userDeliveryAddressService.sendDeliveryAddressTmpMsg(uid, gift, msg);
                    if (childResp.getCode() != HttpStatus.OK.value()) {
                        childResp.setData(uid);
                    }
                    childResps.add(childResp);
                }
            }
            finalResp.setData(childResps);
            return finalResp;
        } else {
            return ResponseV2.fail("转换失败");
        }

    }

    @ResponseBody
    @RequestMapping("/getRankUsers")
    public ResponseV2 getRankUsers(Long start, Long end) {
        return userService.getRankForAdmin(start, end);
    }

    @ResponseBody
    @RequestMapping("/clearTmpMsgRecord")
    public ResponseV2 clearTmpMsgRecord(String redisKey) {
        return userDeliveryAddressService.clearTmpMsgRecord(redisKey);
    }

}
